package cn.ecnu.controller;

import cn.ecnu.common.IM.TLSSigAPIv2;
import cn.ecnu.common.constant.BusinessConstant;
import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.ConsultRecordMapper;
import cn.ecnu.pojo.ConsultRecord;
import cn.ecnu.vo.Converter;
import cn.ecnu.vo.toFront.ConsultRecordVo;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static cn.ecnu.common.constant.BusinessConstant.SDK_APP_ID;
import static cn.ecnu.common.constant.BusinessConstant.SDK_KEY;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Resource
    private ConsultRecordMapper consultRecordMapper;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private Converter converter;

    private TLSSigAPIv2 helper = new TLSSigAPIv2(SDK_APP_ID, SDK_KEY);


    @GetMapping("/getConsultRecord/{id}")
    public R getConsultRecord(@PathVariable("id") Integer id) {
        try {
            ConsultRecordVo vo = converter.getConsultRecordVo(id);
            return R.ok().put("data", vo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getAllConsultRecords")
    public R getAllConsultRecords() {
        try {
            List<ConsultRecord> records = consultRecordMapper.getAllConsultRecord();
            ArrayList<ConsultRecordVo> result = new ArrayList<>();
            for (ConsultRecord record : records) {
                result.add(converter.getConsultRecordVo(record.getConsultRecordId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getConsultRecordsByVisitorId/{id}")
    public R getConsultRecordsByVisitorId(@PathVariable("id") Integer id) {
        try {
            List<ConsultRecord> records = consultRecordMapper.getConsultRecordByVisitorId(id);
            ArrayList<ConsultRecordVo> result = new ArrayList<>();
            for (ConsultRecord record : records) {
                result.add(converter.getConsultRecordVo(record.getConsultRecordId()));
            }
            return R.ok().put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @PostMapping("/getMessage")
    public R getMessage(@RequestBody HashMap<Object, Object> body) {
        try {
            ArrayList<Object> list = new ArrayList<>();
            //https://console.tim.qq.com/v4/openim/admin_getroammsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
            Long sdkappid = BusinessConstant.SDK_APP_ID;
            String identifier = "administrator";
            String usersig = helper.genUserSig(identifier, 86400);
            String url = "https://console.tim.qq.com/v4/openim/admin_getroammsg?sdkappid=" + sdkappid + "&identifier=" + identifier + "&usersig=" + usersig + "&random=99999999&contenttype=json";
            String response = restTemplate.postForObject(url, JSON.toJSON(body), String.class);
            list.add(JSON.toJSON(response));
            //不断续拉
            HashMap map = JSON.parseObject(response, HashMap.class);
            while ((int) (map.get("Complete")) == 0) {
                String lastMsgKey = (String) map.get("LastMsgKey");
                Integer lastMsgTime = (Integer) map.get("LastMsgTime");
                body.put("LastMsgKey", lastMsgKey);
                body.put("MaxTime", lastMsgTime);
                response = restTemplate.postForObject(url, JSON.toJSON(body), String.class);
                list.add(JSON.toJSON(response));
                map = JSON.parseObject(response, HashMap.class);
            }
            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }


}
